<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../statics/layui/css/layui.css">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<form class="layui-form searchForm">
    <div class="layui-form-item">

        <div class="layui-inline">
            <label class="layui-form-label" style="width: 100px;">字典名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">字典值：</label>
            <div class="layui-input-inline">
                <input type="text" name="value" id="value" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">字典编码：</label>
            <div class="layui-input-inline">
                <input type="text" name="code" id="code" autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-inline">
            <label class="layui-form-label" style="width: 100px;">所属分组：</label>
            <div class="layui-input-inline">
                <select class="formData" name="groupId" id="groupId" >
                    <option value="">直接选择或搜索</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <button class="layui-btn layui-btn-sm" id="queryBut" lay-submit lay-filter="formDemo" >搜索</button>
                <button class="layui-btn layui-btn-sm" >清空</button>
            </div>
        </div>
    </div>
</form>
<table class="layui-hide" id="dataList" lay-filter="dataList"></table>
<script type="text/html" id="toolbarOrg">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加数据
        </button>
        <button class="layui-btn layui-btn-sm" lay-event="delList">批量删除</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="groupManage">字典分组管理</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit" title="编辑数据">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/javascript" src="../../../statics/layui/layui.js"></script>
<script src="/static/js/main.js"></script>
<!--<script src="../../../statics/js/config.js" charset="utf-8"></script>-->
<!--<script src="../../../statics/js/common.js" charset="utf-8"></script> -->
<!--<script src="../../../statics/js/sys/dictList.js" charset="utf-8"></script> -->
<!--<script src="../../../statics/js/xadmin.js" charset="utf-8"></script>-->
<script>
    var ysb = {};
    layui.use(['table', 'sessionFun', 'configure', 'configureCom'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        var sessionFun = layui.sessionFun;
        var matchDict = layui.matchDict;
        var configureCom = layui.configureCom;
        var configure = layui.configure;
        var token = sessionFun.get(configure.SESSION_TOKEN) ? sessionFun.get(configure.SESSION_TOKEN).token : '';
        ysb.init = function () {
            var name = "", value = "", groupId = "";
            var cols = [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'name',width:220, title: '字典名称'}
                , {field: 'value',width:120, title: '字典值'}
                , {field: 'groupName',width:220, title: '所属分组'}
                , {field: 'code', width:250,title: '字典编码'}
                , {field: 'status', width:90,title: '字典状态',templet: function (d) {
                        return matchDict.name(d.status, configure.USE_STATUS);
                    }}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]];
            configureCom.tableRender(baseApi + '/sys/dict/list', {}, cols, 'dataList', 10, 'full-200', 'toolbarOrg')
            configureCom.tableSearch();
            configureCom.selectFun('', '', '#groupId',configure.DICT_QUERY_HTTP);
        };
        //删除数据
        ysb.delDict = function (ids) {
            layer.load(1);
            $.ajax({
                type: "POST",
                url: baseApi + "/sys/dict/delete",
                data: {"token": token, "ids": ids},
                success: function (data) {
                    var code = data.code;
                    var msg = data.msg;
                    if (code == "0" || msg == "success") {
                        layer.msg("数据操作成功");
                        configureCom.tableReload(true);
                    } else {
                        layer.msg(data.msg);
                    }
                    layer.closeAll('loading');
                }
            });
            return false;
        };

        //头工具栏事件
        table.on('toolbar(dataList)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            sessionFun.remove(configure.SESSION_LIST_DATA);
            switch (obj.event) {
                case 'add':
                    editDetail('字典管理',configure.DICT_SAVE_HTTP);
                    break;
                case 'delList':
                    var data = checkStatus.data;
                    var ids = "";
                    if (data != null && data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            if (ids == "") {
                                ids = data[i].id;
                            }
                            else {
                                ids += "," + data[i].id;
                            }
                        }
                        layer.confirm('您真的要删除选中的数据吗？', function (index) {
                            ysb.delDict(ids);
                        });
                    } else {
                        layer.msg("请您至少选择一条数据后再提交");
                    }
                    break;
                case 'groupManage':
                    var title = '字典组管理';
                    var url = configure.DICT_GROUP_LIST_HTML;
                    top.layer.open({
                        type: 2,
                        title:title,
                        area: configureCom.Psize1,
                        content: configureCom.nocache(url)
                    });
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(dataList)', function (obj) {
            var data = obj.data;
            sessionFun.set(configure.SESSION_LIST_DATA, data);
            if (obj.event === 'del') {
                layer.confirm('您真的要删除这条数据吗？', function (index) {
                    ysb.delDict(data.id);
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                editDetail('字典管理',configure.DICT_UPDATE_HTTP);
            }
        });
        function editDetail(title,url){
            top.layer.open({
                type: 2,
                area: configureCom.Psize1,
                content: configureCom.nocache(configure.DICT_DETAIL_HTML),
                btn: ['保存'],
                yes: function (index, layero) {
                    var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对
                    var data = iframeWin.getData();
                    configureCom.postDataAjax(url, data).then(function (res) {
                        if (res.code == 0) {
                            top.layer.close(index);
                            configureCom.tableReload(true);
                        }else{
                            top.layer.alert('保存失败！')
                        }
                    });
                }
            })
        }
        //初始化数据
        ysb.init();
    });


</script>
</body>
</html>